<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Project extends PC_controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Projectmodel','m_project');
		$this->load->model('Templatemodel','m_template');
		$this->load->model('Widgetmodel','m_widget');
		$this->load->model('Frameworkmodel','m_framework');
		$this->load->model('Codemodel','m_code');
	}

	public function index(){
		$data = parent::index();
		$this->load->view('project',$data);
	}
/*
| -------------------------------------------------------------------
|  PROJECT
| -------------------------------------------------------------------
*/
	public function getProjectList(){
		$assist['where'] = getFilter($this->input->post(),Projectmodel::$searchField);
		$assist['like'] = getFilter($this->input->post(),Projectmodel::$likeField);
		$assist['pagination'] = getPagination($this->input->post());
		list($list,$count) = $this->m_project->getProjectListSection($assist);

		foreach($list as &$record){
			$record['sprite_exists'] = file_exists(UPLOAD_PATH.$record['project_code'].'/sprite.png');
			$record['id'] = urlencode(base64_encode(SALT.$record['project_id']));
		}

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->responseExit();
	}

	public function createProject(){
		$this->validate->setRule('project_name','string');
		$this->validate->setRule('project_name','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('project_code','string');
		$this->validate->setRule('project_code','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('project_type','integer');
		$this->validate->setRule('project_type','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('project_device_width','integer');
		$this->validate->setRule('project_device_width','notnull',false);
		$this->validate->setRule('project_device_width','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$info['project_code'] = str_replace('.', '_', $info['project_code']);
		if(!$this->m_project->checkNameAvailable($info['project_name'])){
			
			$this->response->setDetail($this->lang->line('error_project_name_used'));
			$this->response->responseExit();
		}

		if(!$this->m_project->checkCodeAvailable($info['project_code'])){
			
			$this->response->setDetail($this->lang->line('error_project_code_used'));
			$this->response->responseExit();
		}

		$code_list = $this->m_code->getNeedCodeList();
		$code_list = extractColumn($code_list,'code_id');

		global $common_static_file_list;
		$info['project_time_create'] = NOW;
		$info['project_commonfile'] = json_encode(array_slice($common_static_file_list, 0, 1));
		$info['project_sprite_width'] = 1000;
		$info['project_sprite_height'] = 500;
		$info['project_status'] = STATUS_ACTIVE;
		$info['project_code_ids'] = json_encode($code_list);
	
		$project_id = $this->m_project->createProject($info);


		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function editProject(){
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('project_type','integer');
		$this->validate->setRule('project_type','message',$this->lang->line('error_parameter'));

		$this->validate->setRule('project_name','string');
		$this->validate->setRule('project_name','message',$this->lang->line('error_parameter'));
		
		$this->validate->setRule('project_version','string');
		$this->validate->setRule('project_version','notnull',false);
		$this->validate->setRule('project_version','message',$this->lang->line('error_parameter'));
		
		$this->validate->setRule('project_device_width','integer');
		$this->validate->setRule('project_device_width','notnull',false);
		$this->validate->setRule('project_device_width','message',$this->lang->line('error_parameter'));

		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();

		if(!$this->m_project->checkNameAvailable($info['project_name'],$info['project_id'])){
			
			$this->response->setDetail($this->lang->line('error_project_name_used'));
			$this->response->responseExit();
		}

		$this->m_project->editProject($info['project_id'],$info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function activeProject(){
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$this->m_project->editProject($info['project_id'],array(
			'project_status' => STATUS_ACTIVE,
		));

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function disableProject(){
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$this->m_project->editProject($info['project_id'],array(
			'project_status' => STATUS_DISABLE,
		));

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}
/*
| -------------------------------------------------------------------
|  CSS & JS
| -------------------------------------------------------------------
*/
	public function getProjectCss(){
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$item = $this->m_project->getProjectCss($info['project_id']);

		$res = array();
		$res['project_css_id'] = 0;
		$res['project_css_content'] = '';
		if(!empty($item)) {
			$res['project_css_id'] = $item['project_css_id'];
			$res['project_css_content'] = $item['project_css_content'];
		}

		$this->response->setSuccess();
		$this->response->setAddon($res);
		$this->response->responseExit();
	}

	public function editProjectCss(){
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('project_css_id','integer');
		$this->validate->setRule('project_css_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('project_css_content','string');
		$this->validate->setRule('project_css_content','notnull',false);
		$this->validate->setRule('project_css_content','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$project = $this->m_project->getProject($info['project_id']);
		if($project['project_css_id'] != $info['project_css_id']){
			
			$this->response->setDetail($this->lang->line('error_edited'));
			$this->response->responseExit();
		}

		unset($info['project_css_id']);
		$info['project_css_time_create'] = NOW;
		$project_css_id = $this->m_project->createProjectCss($info);
		$this->m_project->editProject($info['project_id'],array('project_css_id'=>$project_css_id));

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->setAddon(array('project_css_id'=>$project_css_id));
		$this->response->responseExit();
	}

	public function getProjectJs(){
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$item = $this->m_project->getProjectJs($info['project_id']);

		$res = array();
		$res['project_js_id'] = 0;
		$res['project_js_content'] = '';
		if(!empty($item)){
			$res['project_js_id'] = $item['project_js_id'];
			$res['project_js_content'] = $item['project_js_content'];
		}

		$this->response->setSuccess();
		$this->response->setAddon($res);
		$this->response->responseExit();
	}

	public function editProjectJs(){
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('project_js_id','integer');
		$this->validate->setRule('project_js_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('project_js_content','string');
		$this->validate->setRule('project_js_content','notnull',false);
		$this->validate->setRule('project_js_content','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$project = $this->m_project->getProject($info['project_id']);
		if($project['project_js_id'] != $info['project_js_id']){
			
			$this->response->setDetail($this->lang->line('error_edited'));
			$this->response->responseExit();
		}

		unset($info['project_js_id']);
		$info['project_js_time_create'] = NOW;
		$project_js_id = $this->m_project->createProjectJs($info);
		$this->m_project->editProject($info['project_id'],array('project_js_id'=>$project_js_id));

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->setAddon(array('project_js_id'=>$project_js_id));
		$this->response->responseExit();
	}
/*
| -------------------------------------------------------------------
|  FILE
| -------------------------------------------------------------------
*/
	// Define the custom sort function
	static function custom_sort($a,$b) {
	        return $a['project_file_time_lastmodified']<$b['project_file_time_lastmodified'];
	}

	public function getProjectFileList(){
		$this->validate->setRule('project_code','string');
		$this->validate->setRule('project_code','message',$this->lang->line('error_parameter'));

		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		//$file_pos = UPLOAD_PATH.$project['project_code'].'/'.$info['project_file']['name'][$i];
		$info = $this->validate->getResult();
	
		$images = @scandir(UPLOAD_PATH.$info['project_code']);
		$list = array();
		if($images) {
			foreach ($images as $img) {
				$file = UPLOAD_PATH.$info['project_code'].'/'.$img;
				if(is_dir($file)) continue;

				$size = getimagesize($file);
				
				
				$list[] = array(
					'project_file_name' => $img,
					'project_file_path' => $file,
					'project_file_html' => ($img=='sprite.png')?
					'<span style="color:#800;">background:transparent url('.str_replace(dirname(BASEPATH),'',UPLOAD_PATH).$info['project_code'].'/'.$img.') no-repeat 0 0;</span>':htmlspecialchars('<img src="'.str_replace(dirname(BASEPATH),'',UPLOAD_PATH).$info['project_code'].'/'.$img.'" '.$size[3].' />'),
					'project_file_time_lastmodified' => date("Y-m-d H:i:s", filemtime($file)),
					'project_file_url' => UPLOAD_URL.$info['project_code'].'/'.$img,
					'project_file_size' => round((filesize($file) / 1024), 2),
				);

			}
		}
		

		usort($list, array("Project", "custom_sort"));
     	

	

		
		$count = count($list);

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->responseExit();
	}



	public function addImage($project_id = 0){
		if(!$this->m_app->initFolder()){
			$this->response->setDetail($this->lang->line('error_update'));
			$this->response->responseExit();
		}

		$project = $this->m_project->getProject($project_id);
		if(empty($project)) {
			$this->response->setDetail('项目错误');
			$this->response->responseExit();
		}
		$old = umask(0);
		@mkdir(UPLOAD_PATH.$project['project_code'].'/',0777,true);
		umask($old);

		$existed_sprite = $this->m_project->getProjectSpriteList($project_id);
		$existed_sprite = reindexArray($existed_sprite,'sprite_name');

		$filename = (isset($_SERVER['HTTP_X_FILENAME']) ? $_SERVER['HTTP_X_FILENAME'] : 'a.jpg');
		$path = $project['project_code'].'/'.$filename;
		$file_pos = UPLOAD_PATH.$path;
		file_put_contents($file_pos,file_get_contents('php://input'));
		
		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function removeProjectFile(){
		$this->validate->setRule('project_file_path','string');
		$this->validate->setRule('project_file_path','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		
		if(file_exists($info['project_file_path'])){
			unlink($info['project_file_path']);
		}
		

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function editProjectFileGet(){
		$this->validate->setRule('project_file_id','integer');
		$this->validate->setRule('project_file_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		
		$info = $this->validate->getResult();
		$project_file = $this->m_project->getProjectFile($info['project_file_id']);
		if(empty($project_file) || !file_exists(UPLOAD_PATH.$project_file['project_file_path']) || $project_file['project_file_status'] != STATUS_ACTIVE){
			
			$this->response->setDetail($this->lang->line('error_file'));
			$this->response->responseExit();
		}

		$update_info = array(
			'user_id' => $this->m_app->getCurrentUserId(),
			'project_file_status' => STATUS_PENDING,
		);
		$this->m_project->editProjectFile($info['project_file_id'],$update_info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

/*
| -------------------------------------------------------------------
|  COMMON FILE
| -------------------------------------------------------------------
*/
	public function getCommonFileList(){
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$project = $this->m_project->getProject($info['project_id']);
		$commonFile = empty($project)?'[]':$project['project_commonfile'];
		$commonFile = validate($commonFile)?$commonFile:'[]';
		$commonFile = json_decode($commonFile);
		global $common_static_file_list;
		$list = array();
		foreach($common_static_file_list as $file){
			$file_status = in_array($file,$commonFile)?1:0;
			$list[] = array(
				'file_name' => $file,
				'file_status' => $file_status,
			);
		}
		$count = count($list);

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->responseExit();
	}

	public function editCommonFileList(){
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('common_file','array');
		$this->validate->setRule('common_file','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		global $common_static_file_list;
		$list = array();
		foreach($common_static_file_list as $file){
			if(in_array($file,$info['common_file'])){
				$list[] = $file;
			}
		}
		$this->m_project->editProject($info['project_id'],array('project_commonfile'=>json_encode($list)));

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}
/*
| -------------------------------------------------------------------
|  EXPORT
| -------------------------------------------------------------------
*/
	public function downloadProjectStyle($project_code,$only_page_css=0,$python=0){
		$project = $this->m_project->getProjectByCode($project_code);

		if(empty($project)) show_404();

		if(!empty($project['project_version'])) {
			$project['project_version'] = '$'.$project['project_version'];
		}
		
		$this->load->library('zip');
		
		$proj_css = $this->m_project->getProjectCss($project['project_id']);
		$project_css_content = empty($proj_css)?'':$proj_css['project_css_content'];
		$project_css_content = $project_css_content;
		$proj_js = $this->m_project->getProjectJs($project['project_id']);
		$project_js_content = empty($proj_js)?'':$proj_js['project_js_content'];
		$project_css_content = str_replace('/uploads/'.$project['project_code'].'/','',$project_css_content);	

		//add code js css by klj 2014-06-11
		$code_list = json_decode($project['project_code_ids'],true);
		if(!empty($code_list)) {
			$code_list = $this->m_code->getCodeByIds($code_list);
		}
		$code_css = '';
		$code_js = '';
		foreach ($code_list as $key => $value) {
			$code_css .= str_replace('/uploads/code_image/','code_image/',$value['code_content_css']);
			$code_js .= str_replace('/uploads/code_image/','/resource/'.$project['project_code'].$project['project_version'].'/code_image/',$value['code_content_js']);
			$file_list = scandir(CODE_IMAGE_PATH.$value['code_id']);
			foreach($file_list as $file){
				$path = CODE_IMAGE_PATH.$value['code_id'].'/'.$file;
				if(!is_dir($path)&&file_exists($path)&&!$only_page_css){
					$data = file_get_contents($path);
					$this->zip->add_data($project['project_code'].$project['project_version'].'/code_image/'.$value['code_id'].'/'.$file,$data);
				}
			}
		}
		$project_js_content = $code_js.$project_js_content;
		$project_css_content = $code_css.$project_css_content;

		global $compression_replace;
		$this->load->library('codecompression');
		$project_js_content = $this->codecompression->jscompression($project_js_content);
		$project_css_content = $this->codecompression->csscompression($project_css_content);

		$commonFile = validate($project['project_commonfile'])?json_decode($project['project_commonfile']):array();
		
	
		$file_list = file_exists(UPLOAD_PATH.$project['project_code']) ? scandir(UPLOAD_PATH.$project['project_code']) : array();
		$template_list = $this->m_template->getTemplateListByProject($project['project_id']);
		

		if(!$only_page_css){
			$this->zip->add_data($project['project_code'].$project['project_version'].'/jscript.js',$project_js_content);
			$this->zip->add_data($project['project_code'].$project['project_version'].'/style.css',$project_css_content);	
		}
		
		foreach($commonFile as $file){
			$path = COMMON_PATH.$file;
			if(file_exists($path)&&!$only_page_css){
				$data = file_get_contents($path);
				$this->zip->add_data($project['project_code'].$project['project_version'].'/common/'.$file,$data);
			}
		}
		foreach($file_list as $file){
			$path = UPLOAD_PATH.$project['project_code'].'/'.$file;

			if(!is_dir($path)&&file_exists($path)&&!$only_page_css){
				$data = file_get_contents($path);
				$this->zip->add_data($project['project_code'].$project['project_version'].'/'.$file,$data);
			}
		}
		if(file_exists(UPLOAD_PATH.$project['project_code'].'/sprite.png')&&!$only_page_css){
			$data = file_get_contents(UPLOAD_PATH.$project['project_code'].'/sprite.png');
			$this->zip->add_data($project['project_code'].$project['project_version'].'/sprite.png',$data);	
		}
		foreach($template_list as $template){
			$data = '';
		
			$widget_list = $this->m_widget->getWidgetListByStruct($template['template_struct'],$template['project_id']);
			foreach ($widget_list as $widget) {
				$data .= $widget['widget_css'];
			}
			
			$data .= $template['template_css'];
			$data = str_replace($compression_replace,'',$data);
			$data = str_replace('/uploads/'.$project['project_code'],($python == 0 ? '<?php echo RESOURCE_URL.TEMPLATE_PROJECT ?>': '{{cf_config.RESOURCE_URL + cf_config.TEMPLATE_PROJECT}}'),$data);
			$data = '<style>'.$data.'</style>'."\n";
			if($python == 0){
				$data .= "<?php /*\n";
				$data .= '---TEMPLATE:'.$template['template_name']."---\n";
				$data .= '---EXPORT TIME:'.NOW."---\n";
				$data .= $template['template_struct'].'*/';
			}
			$this->zip->add_data($project['project_code'].$project['project_version'].'/templateCSS/'.$template['template_name'].($python == 0 ? '.php': '.css'),$data);
		}
		//添加样式版本号
		$this->zip->add_data($project['project_code'].$project['project_version'].'/ZM_VERSION.php','<?php $ZM_VERSION = \''.$project['project_version'].'\'; ?>');
		$this->zip->download($project['project_code'].'.zip');
	}

	#用于手动下载静态HTML
	public function dp($code){
		$id = str_replace(SALT, '', base64_decode(urldecode($code)));
		$this->downloadStaticPackage($id);
	}
	public function downloadStaticPackage($project_id){
		$project = $this->m_project->getProject($project_id);
		$template_list = $this->m_template->getTemplateListByProject($project_id);
		$this->load->library('zip');
		foreach($template_list as $template){
			$view = file_get_contents(base_url().'t/'.$template['template_id']);
			$view = str_replace('/uploads/'.$project['project_code'].'/','assets/',$view);
			$view = str_replace('/favicion.ico','/assets/favicion.ico',$view);
			$this->zip->add_data($template['template_name'].'.html',$view);
			unset($view);
		}

		$commonFile = validate($project['project_commonfile'])?json_decode($project['project_commonfile']):array();


		$this->load->library('zip');

		foreach($commonFile as $file){
			$path = COMMON_PATH.$file;
			if(file_exists($path)){
				$data = file_get_contents($path);
				$this->zip->add_data('assets/common/'.$file,$data);
			}
		}
		$file_list = scandir(UPLOAD_PATH.$project['project_code']);
		foreach($file_list as $file){
			$path = UPLOAD_PATH.$project['project_code'].'/'.$file;
			
			if(file_exists($path)&& !is_dir($path)){
				$data = file_get_contents($path);
				$this->zip->add_data('assets/'.$file,$data);
			}
		}
		if(file_exists(UPLOAD_PATH.$project['project_code'].'/sprite.png')){
			$data = file_get_contents(UPLOAD_PATH.$project['project_code'].'/sprite.png');
			$this->zip->add_data('assets/sprite.png',$data);
		}

		$this->zip->download($project['project_code'].'_'.date('YmdHis').'.zip');
	}

	public function getSprite(){
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		$project = $this->m_project->getProject($info['project_id']);
		if(empty($project)){
			
			$this->response->setDetail($this->lang->line('error_parameter'));
			$this->response->responseExit();
		}

		$list = $this->m_project->getProjectSpriteList($info['project_id']);
		foreach($list as $key => &$record){
			$record['sprite_path'] = UPLOAD_PATH.$record['sprite_path'];
			if($record['sprite_status'] != STATUS_ACTIVE || !file_exists($record['sprite_path'])) {
				unset($list[$key]);
				continue;	
			}
			$info = getimagesize($record['sprite_path']);
			$record['sprite_width'] = $info[0];
			$record['sprite_height'] = $info[1];
			$record['sprite_css'] = "[".pathinfo($record['sprite_name'],PATHINFO_FILENAME)."]\nW:".$record['sprite_width']." H:".$record['sprite_height']."\n\n-".$record['sprite_x']."px -".$record['sprite_y']."px\n";
		}
        $list = array_values($list);
		$count = count($list);

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->setAddon(array(
			'sprite_url' => UPLOAD_URL.$project['project_code'].'/sprite.png',
		));
		$this->response->responseExit();
	}
/*
| -------------------------------------------------------------------
|  CODE
| -------------------------------------------------------------------
*/
	public function getCodeList(){
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$project = $this->m_project->getProject($info['project_id']);
		if(empty($project)) {
			$this->response->setDetail('参数错误');
			$this->response->responseExit();
		}

		$assist['where'] = getFilter($this->input->post(),Codemodel::$searchField);
		$assist['like'] = getFilter($this->input->post(),Codemodel::$likeField);
		$list = $this->m_code->getCodeList($assist);
		$count = count($list);

		foreach ($list as $key => $value) {
			$list[$key]['code_linked'] = strpos($project['project_code_ids'], '"'.$value['code_id'].'"')>0;
		}

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->responseExit();
	}

	public function editLinkedCode() {
		$this->validate->setRule('project_id','integer');
		$this->validate->setRule('project_id','message',$this->lang->line('error_parameter'));
		$this->validate->setRule('project_code_ids','array');
		$this->validate->setRule('project_code_ids','optional');
		$this->validate->setRule('project_code_ids','message',$this->lang->line('error_parameter'));
		if(!$this->validate->run($this->input->post())){
			
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}

		$info = $this->validate->getResult();
		if(!isset($info['project_code_ids'])) {
			$info['project_code_ids'] = array();
		}
		$info['project_code_ids'] = json_encode($info['project_code_ids']);
		$this->m_project->editProject($info['project_id'],$info);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}
}
/* End of file project.php */
/* Location: ./application/controllers/project.php */
